﻿namespace com.stifel.BranchMgmt.WebParts.PromoteInfo
{
    using System;
    using System.Web;
    using System.Web.Caching;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using com.stifel.BranchMgmtDao;
    using com.stifel.Extensions.StringExtensions;

    using Microsoft.SharePoint;

    public partial class PromoteInfoUserControl
    {
        #region Fields

        string _UrlReferrer;

        #endregion Fields

        #region Properties

        string UrlReferrer
        {
            get
            {
                if ( string.IsNullOrEmpty( _UrlReferrer ) )
                    _UrlReferrer = ( string ) Page.Cache.Get( CacheKey.UrlReferrer );
                if ( string.IsNullOrEmpty( _UrlReferrer ) )
                    setUrlReferrer( );
                return _UrlReferrer;
            }
            set
            {
                _UrlReferrer = value;
                addToCache( CacheKey.UrlReferrer, value );
            }
        }

        #endregion Properties

        #region Methods

        void addToCache( string key, object value )
        {
            if ( value == null )
                Cache.Remove( key );
            else
                // cache for 10 hours
                Cache.Insert( key, value, null,
                    Cache.NoAbsoluteExpiration,
                    new TimeSpan( 10, 0, 0 ),
                    CacheItemPriority.Normal,
                    new CacheItemRemovedCallback(
                        delegate( string k, object v, CacheItemRemovedReason reason )
                        {
                        } ) );
        }

        void hidePromotingMsgAndEnablePromote( )
        {
            uiLblPromoteMsg.Visible = false;
            uiBtnPromote.Enabled = true;
        }

        bool isNotPromoting( Guid uniqueId )
        {
            StifelBranchPromotionWorklistDao dao = new StifelBranchPromotionWorklistDao( SPContext.Current.Web.Url );
            StifelBranchPromotionWorklist o = dao.GetByTitle( uniqueId );
            if ( o == null )
                return true;
            else
            {
                DateTime a = DateTime.Now;
                DateTime b = o.Modified.AddMinutes( 15 );
                return a.CompareTo( b ) > 0;
            }
        }

        bool isValidRequest( Guid uniqueId )
        {
            StifelBranchInfoDao dao = new StifelBranchInfoDao( SPContext.Current.Web.Url );
            StifelBranchInfo o = dao.Get( uniqueId );
            return o == null ? false : true;
        }

        void navigateToSelectedView( Button button )
        {
            string arg = button.CommandArgument;
            int index = -1;

            for ( int i = 0; i < uiView.Views.Count; i++ )
            {
                View view = uiView.Views[ i ];
                if ( view.ID.Equals( arg, StringComparison.OrdinalIgnoreCase ) )
                {
                    index = i;
                    break;
                }
            }
            uiView.ActiveViewIndex = index >= 0 ? index : uiView.Views.Count - 1;
            foreach ( Control c in uiNavigation.Controls )
            {
                Button b = c as Button;
                if ( b != null )
                {
                    b.Attributes.Remove( Attribute.Current );
                }
            }
            button.Attributes.Add( Attribute.Current, true.ToString( ) );
        }

        void setDefaultView( )
        {
            Button button = uiNavigation.FindControl( "uiBtnBasics" ) as Button;
            navigateToSelectedView( button );
            uiInfo.Visible = true;
            uiMsg.Visible = false;
        }

        void setUrlReferrer( )
        {
            if ( Request != null && Request.UrlReferrer != null )
                UrlReferrer = Request.UrlReferrer.ToString( );
            else if ( Request != null )
                UrlReferrer = Request.Url.ToString( );
        }

        void showPromotingMsgAndDisablePromote( )
        {
            uiLblPromoteMsg.Visible = true;
            uiBtnPromote.Enabled = false;
        }

        #endregion Methods

        #region Nested Types

        class Attribute
        {
            #region Fields

            public const string Current = "current";

            #endregion Fields
        }

        class CacheKey
        {
            #region Fields

            public const string UrlReferrer = "r";

            public static string[ ] Values = { UrlReferrer };

            #endregion Fields
        }

        #endregion Nested Types
    }
}